<template>
  <el-dialog
    :title="modelType == 1 ? '添加用户' : '编辑用户信息'"
    :visible.sync="formData.dialogVisible"
    width="40%"
    :before-close="handleClose"
  >
    <el-form
      :model="userInfo"
      :rules="rules"
      ref="userForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="userInfo.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model.number="userInfo.age"
          placeholder="请输入年龄"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="userInfo.sex" placeholder="请选择">
          <el-option label="男" :value="1"></el-option>
          <el-option label="女" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期">
        <el-col :span="11">
          <el-form-item prop="birth">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="userInfo.birth"
              style="width: 100%"
              format="yyyy-MM-DD"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="地址" prop="addr" placeholder="请输入地址">
        <el-input v-model="userInfo.addr"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "AddUpdate",
  data() {
    return {
      userInfo: {
        name: "",
        age: 18,
        sex: "",
        birth: "",
        addr: "",
        id:''
      },
      rules: {
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { max: 10, message: "长度小于10", trigger: "blur" },
        ],
        age: [
          { required: true, message: "请输入年龄", trigger: "blur" },
          { type: "number", message: "年龄必须为数值", trigger: "blur" },
        ],
        sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        addr: [{ required: true, message: "请输入地址", trigger: "blur" }],
        birth: [{ required: true, message: "请输入出生日期", trigger: "blur" }],
      },
    };
  },
  props: ["editRow"],
  mounted(){
    this.initForm()
  },
  computed: {
    ...mapState("user", ["formData", "modelType"]),
  },
  methods: {
    handleClose() {
      this.$store.commit("user/CANCELDIALOG");
    },
    submit() {
      // 提交表单：当所有表单验证都通过时
      this.$refs.userForm.validate(async (valid) => {
        let res = null
        if (valid) {
          if (this.modelType == 1) { //添加用户
            res = await this.$store.dispatch("user/addUser", this.userInfo);
            // this.$store.commit("user/CANCELDIALOG");
            this.$refs.userForm.resetFields();
          }else{   //更新用户
            // 修改数据
            res = await this.$store.dispatch('user/updateUser',this.editRow)
            // 重置字段
            this.userInfo = {
                  name: "",
                  age: 18,
                  sex: "",
                  birth: "",
                  addr: "",
                }
            this.$refs.userForm.resetFields();
          }

        // 修改或者添加成功，需要更新页面数据
        res && this.$emit('getUser')

        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    initForm() {
      if (!this.modelType) {
        // 深度拷贝
        this.userInfo = this.editRow
      }
    },
  },
  watch:{
    editRow(){
      this.initForm()
    }
  }
};
</script>

<style>
</style>